<template>
  <div class="container">
    <el-card>
      <el-form size="mini">
        <el-form-item label="学科名称">
          <el-input
            @keyup.enter="searchList"
            v-model="subjectName"
            style="width: 200px"
            placeholder="请输入学科名称"
            class="filter-item"
          ></el-input>
          <el-button
            class="filter-item"
            style="margin-left: 10px"
            size="mini"
            @click="clearInfo"
            >清除</el-button
          >
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-search"
            @click="searchList"
            >搜索</el-button
          >
          <el-button
            class="filter-item fr"
            size="small"
            style="margin-left: 10px"
            type="success"
            icon="el-icon-edit"
            @click="putAddSubject"
            >新增学科</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 提示信息 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        type="info"
        class="alert"
        style="margin-bottom: 10px"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- 表格 -->
      <el-table
        :data="dataList"
        v-loading="loading"
        element-loading-text="给我一点时间"
        fit
        highlight-current-row
        style="width: 100%; font-size: 13px"
      >
        <el-table-column
          align="center"
          type="index"
          label="序号"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="subjectName"
          label="学科名称"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="username"
          label="创建者"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="addDate"
          width="180"
          label="创建日期"
          :formatter="formatTime"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="isFrontDisplay"
          label="前台是否显示"
        >
          <template slot-scope="{ row }">
            <span v-if="row.isFrontDisplay === 1">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="twoLevelDirectory"
          label="二级目录"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="tags"
          label="标签"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="totals"
          label="题目数量"
        ></el-table-column>
        <el-table-column align="center" width="220" label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              class="smallText"
              @click="$router.push(`directorys?id=${row.id}&name=${row.subjectName}`)"
              >学科分类</el-button
            >
            <el-button
              type="text"
              class="smallText"
              @click="$router.push(`tags?id=${row.id}&name=${row.subjectName}`)"
              >学科标签</el-button
            >
            <el-button type="text" class="smallText" @click="putAddSubject(row)"
              >修改</el-button
            >
            <el-button type="text" class="smallText" @click="delList(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pages" style="margin-top: 10px; text-align: right">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.page"
          :page-size="query.pagesize"
          :total="total"
          :page-sizes="[5, 10, 15, 20, 30]"
          layout="prev, pager, next, sizes, jumper"
        >
        </el-pagination>
      </div>
    </el-card>
    <JFCSubjectsAdd
      :showDialog.sync="showDialog"
      :list.sync="list"
      :subject="subject"
    />
  </div>
</template>

<script>
import { getList, delList } from '@/api/hmmm/subjects'
import { parseTime } from '@/utils'
import JFCSubjectsAdd from '../components/jfc-subjects-dialog.vue'
export default {
  name: 'Subjects',
  components: {
    JFCSubjectsAdd
  },
  data () {
    return {
      total: 0,
      loading: false,
      dataList: [],
      query: {
        page: 1,
        pagesize: 10
      },
      subjectName: '',
      showDialog: false,
      subject: '',
      list: {
        subjectName: '',
        isFrontDisplay: 1
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 获取学科详情列表
    async getList () {
      this.loading = true
      const { data } = await getList(this.query)
      // console.log(data)
      this.total = data.counts
      this.dataList = data.items
      this.loading = false
    },
    // 搜索
    async searchList () {
      this.loading = true
      const { data } = await getList({
        page: 1,
        pagesize: 10,
        subjectName: this.subjectName
      })
      // console.log(data)
      this.total = data.counts
      this.dataList = data.items
      this.loading = false
    },
    // 删除学科
    async delList (obj) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await delList(obj)
      const { data } = await getList(this.query)
      if (data.items.length === 0 && this.query.page > 1) {
        this.query.page--
        this.getList()
      } else {
        this.total = data.counts
        this.dataList = data.items
      }
    },
    // 添加和修改学科
    async putAddSubject (data) {
      this.showDialog = true
      // this.$children.isFrontDisplay()
      // this.$children.value()
      if (data.id) {
        this.list = {
          id: data.id,
          subjectName: data.subjectName,
          isFrontDisplay: data.isFrontDisplay
        }
        this.subject = '修改学科'
      } else {
        this.subject = '新增学科'
      }
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.query.page = val
      this.getList()
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getList()
    },
    // 清除input框
    clearInfo () {
      this.subjectName = ''
      this.query.page = 1
      this.getList()
    },
    // 时间格式处理
    formatTime (row, column, cellValue, index) {
      return parseTime(cellValue)
    }
  }
}
</script>

<style scoped>
.smallText {
  font-size: 13px;
}
.blue {
  color: #409eff;
}
</style>
